home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / netzwerk / parnet-filesystem / netio.c < prev    next >
C/C++ Source or Header  |  1996-02-26  |  5KB  |  252 lines

  1. /*
  2.  * $Id: netio.c,v 1.3 1995/11/14 00:43:34 Rhialto Exp $
  3.  * $Log: netio.c,v $
  4.  * Revision 1.3  1995/11/14  00:43:34  Rhialto
  5.  * More casts for yet stricter DICE type checking...
  6.  *
  7.  * Revision 1.2  1995/11/14  00:08:15  Rhialto
  8.  * #ifdef -> #if.
  9.  *
  10.  * Revision 1.1  1993/12/02  20:45:46  Rhialto
  11.  * Initial revision
  12.  *
  13.  *
  14.  * Network file system: network I/O. This version for PARNET.DEVICE.
  15.  */
  16.  
  17. #include "netfs.h"
  18. #include "devices/parnet.h"
  19.  
  20. #if DEBUG
  21. #   include "syslog.h"
  22. #else
  23. #   define    debug(x)
  24. #endif
  25.  
  26. Prototype struct MsgPort *Port;
  27. Prototype struct MsgPort *RdPort;
  28. Prototype struct IOParReq *ParReq;
  29. Prototype struct IOParReq *RdParReq[PENDREADS];
  30. Prototype Packet     *RdPkt[PENDREADS];
  31. Prototype int          MyAddr;
  32. Prototype int          PeerAddr;
  33. Prototype int          NetFSPort;
  34.  
  35. struct MsgPort *Port;
  36. struct MsgPort *RdPort;
  37. struct IOParReq *ParReq;
  38. struct IOParReq *RdParReq[PENDREADS];
  39. Packet           *RdPkt[PENDREADS];
  40. int        MyAddr;
  41. int        PeerAddr;
  42. int        NetFSPort;
  43.  
  44. Prototype void SendRead(struct IOParReq *io, Packet *pkt, ULONG size);
  45.  
  46. void
  47. SendRead(struct IOParReq *io, Packet *pkt, ULONG size)
  48. {
  49.     io->io_Command = CMD_READ;
  50.     io->io_Data    = pkt;
  51.     io->io_Length  = size;
  52.     io->io_Data2   = NULL;
  53.     io->io_Length2 = 0;
  54.  
  55.     SendIO((struct IORequest *)io);
  56. }
  57.  
  58. Prototype LONG DoReset(struct IOParReq *io);
  59.  
  60. LONG
  61. DoReset(struct IOParReq *io)
  62. {
  63.     char        pkt[STDREPLY];
  64.     Packet       *p = (Packet *)pkt;
  65.     struct        DateStamp ds;
  66.  
  67.     p->p_Type = pt_Reset;
  68.     p->p_Origin = MyAddr;
  69.  
  70.     DateStamp(&ds);
  71.  
  72.     if (MyValidation == 0)
  73.     MyValidation = (ds.ds_Days * 24*60 +
  74.             ds.ds_Minute) * 60 +
  75.                ds.ds_Tick / TICKS_PER_SECOND;
  76.     p->p_Validation = MyValidation;
  77.  
  78.     debug(("DoReset %d\n", MyValidation));
  79.  
  80.     io->io_Command = CMD_WRITE;
  81.     io->io_Data    = pkt;
  82.     io->io_Length  = sizeof(pkt);
  83.     io->io_Data2   = NULL;
  84.     io->io_Length2 = 0;
  85.  
  86.     return DoIO((struct IORequest *)io);
  87. }
  88.  
  89. Prototype LONG DoResetReply(struct IOParReq *io);
  90.  
  91. LONG
  92. DoResetReply(struct IOParReq *io)
  93. {
  94.     char        pkt[STDREPLY];
  95.     Packet       *p = (Packet *)pkt;
  96.  
  97.     p->p_Type = pt_ResetReply;
  98.     p->p_Origin = MyAddr;
  99.  
  100.     p->p_Validation = MyValidation;
  101.  
  102.     debug(("DoResetReply %d\n", MyValidation));
  103.  
  104.     io->io_Command = CMD_WRITE;
  105.     io->io_Data    = pkt;
  106.     io->io_Length  = sizeof(pkt);
  107.     io->io_Data2   = NULL;
  108.     io->io_Length2 = 0;
  109.  
  110.     return DoIO((struct IORequest *)io);
  111. }
  112.  
  113. Prototype LONG DoProtocol(struct IOParReq *io, Packet *pkt);
  114.  
  115. LONG
  116. DoProtocol(struct IOParReq *io, Packet *pkt)
  117. {
  118.     switch (pkt->p_Type) {
  119.     case pt_Reply:
  120.     return pt_Reply;
  121.     case pt_Request:
  122.     return pt_Request;
  123.     case pt_AsyncReply:
  124.     DoAsyncReply(pkt);      /* Different in client and server */
  125.     return pt_Retry;
  126.     break;
  127.     case pt_Reset:
  128.     /* Oops! Server reset! */
  129.     Validation = pkt->p_Validation;
  130.     DoResetReply(io);
  131.     return pt_Reset;    /* No use in retrying */
  132.     case pt_ResetReply:
  133.     Validation = pkt->p_Validation;
  134.     debug(("got ResetReply %d\n", Validation));
  135.     /* fall through */
  136.     default:
  137.     /* Ignore bad packets, including mistaken pt_Requests */
  138.     debug(("Must re-read a packet\n"));
  139.     return pt_Retry;
  140.     }
  141. }
  142.  
  143. Prototype LONG InitReads(void);
  144.  
  145. LONG
  146. InitReads(void)
  147. {
  148.     int         i;
  149.  
  150.     for (i = 0; i < PENDREADS; i++) {
  151.     if ((RdPkt[i] = AllocMem(PKTSIZE, MEMF_PUBLIC)) == NULL)
  152.         return 4;
  153.     }
  154.  
  155.     for (i = 0; i < PENDREADS; i++) {
  156.     SendRead(RdParReq[i], RdPkt[i], PKTSIZE);
  157.     }
  158.  
  159.     return 0;
  160. }
  161.  
  162. Prototype ULONG OpenNetwork(void);
  163.  
  164. ULONG
  165. OpenNetwork(void)
  166. {
  167.     ULONG        error;
  168.     int         i;
  169.  
  170.     Port = CreatePort("Net:NetIO", -1);
  171.     if (Port == NULL)
  172.     return 1;
  173.     RdPort = CreatePort("Net:RdNetIO", -1);
  174.     if (RdPort == NULL)
  175.     return 1;
  176.     ParReq = (struct IOParReq *)CreateExtIO(Port, sizeof(*ParReq));
  177.     if (ParReq == 0)
  178.     return 2;
  179.  
  180.     ParReq->io_Port = NetFSPort;
  181.     ParReq->io_Flags = PRO_DGRAM;
  182.     ParReq->io_Addr = PeerAddr;
  183.  
  184.     debug(("MyAddr %d PeerAddr %d Port %d\n", MyAddr, PeerAddr, NetFSPort));
  185.  
  186.     error = OpenDevice(DevName, 0, (struct IORequest *)ParReq, 0);
  187.     if (error)
  188.     return error;
  189.  
  190.     ParReq->io_Command = PPD_SETADDR;
  191.     ParReq->io_Addr = MyAddr;
  192.     DoIO((struct IORequest *)ParReq);
  193.  
  194.     ParReq->io_Addr = PeerAddr;
  195.  
  196.     if ((RdParReq[0] = (struct IOParReq *)CreateExtIO(RdPort, sizeof(*ParReq)))
  197.     == NULL)
  198.     return 3;
  199.  
  200.     RdParReq[0]->io_Device = ParReq->io_Device;
  201.     RdParReq[0]->io_Unit   = ParReq->io_Unit;
  202.     RdParReq[0]->io_Port   = NetFSPort;     /* not needed, just tidy */
  203.     RdParReq[0]->io_Flags  = IOF_QUICK;
  204.  
  205.     for (i = 1; i < PENDREADS; i++) {
  206.     if ((RdParReq[i] = (struct IOParReq *)CreateExtIO(RdPort,
  207.                                                       sizeof(*ParReq)))
  208.         == NULL)
  209.         return 4;
  210.     *RdParReq[i] = *RdParReq[0];
  211.     }
  212.  
  213.     InitReads();
  214.     DoReset(ParReq);
  215.  
  216.     return 0;
  217. }
  218.  
  219. Prototype ULONG CloseNetwork(void);
  220.  
  221. ULONG
  222. CloseNetwork(void)
  223. {
  224.     int         i;
  225.  
  226.     if (ParReq) {
  227.     if (ParReq->io_Device) {
  228.         CloseDevice((struct IORequest *)ParReq);
  229.     }
  230.     DeleteExtIO((struct IORequest *)ParReq);
  231.     ParReq = NULL;
  232.     }
  233.     for (i = 0; i < PENDREADS; i++) {
  234.     if (RdParReq[i]) {
  235.         AbortIO((struct IORequest *)RdParReq[i]);
  236.         WaitIO((struct IORequest *)RdParReq[i]);
  237.         DeleteExtIO((struct IORequest *)RdParReq[i]);
  238.     }
  239.     RdParReq[i] = NULL;
  240.     }
  241.     if (RdPort) {
  242.     DeletePort(RdPort);
  243.     RdPort = NULL;
  244.     }
  245.     if (Port) {
  246.     DeletePort(Port);
  247.     Port = NULL;
  248.     }
  249.     return 0;
  250. }
  251.  
  252.